package com.viettel.pon.common;

import com.viettel.maps.base.LatLng;

public class MapFunction {
	public static double calculateDistance(LatLng startPoint, LatLng toPoint) {
		double deltaLat = startPoint.getLatitude() - toPoint.getLatitude();
		double deltaLon = startPoint.getLongitude() - startPoint.getLongitude();
		double distance = Math.sqrt(deltaLat * deltaLat + deltaLon * deltaLon);
		distance = distance * 110000;
		return distance;
	}

	public static int getLineAngle(LatLng startPoint, LatLng toPoint) {
		double deltaLat = startPoint.getLatitude() - toPoint.getLatitude();
		double deltaLon = startPoint.getLongitude() - startPoint.getLongitude();
		double distance = Math.sqrt(deltaLat * deltaLat + deltaLon * deltaLon);
		int x = 1;
		if (deltaLat * deltaLon < 0) {
			x = -1;
		}
		double sinAlpha = x * Math.abs(deltaLat) / distance;
		if (sinAlpha == 0) {
			if (deltaLon > 0) {
				return 180;
			} else {
				return 0;
			}
		} else {
			double alpha = Math.asin(sinAlpha);
			alpha = alpha * 180 / Math.PI;
			// check 4 truong hop
			if (alpha > 0) {
				if (deltaLat > 0 && deltaLon > 0) {
					alpha += 180;
				}
			} else {
				if (deltaLat > 0 && deltaLon < 0) {
					alpha += 360;
				} else {
					alpha += 180;
				}
			}
			return (int) alpha;
		}
	}
}
